package org.lagou.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lagou.Dao.CompanyDao;
import org.lagou.Dao.UserDao;
import org.lagou.Dao.impl.CompanyDaoImpl;
import org.lagou.Dao.impl.UserDaoImpl;
import org.lagou.bean.CompanyBean;
import org.lagou.bean.ReponseBody;
import org.lagou.bean.UserBean;
import org.lagou.tools.Tools;

import com.google.gson.Gson;

@WebServlet("/CompanySevlet")
public class CompanySevlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	CompanyDao dao = new CompanyDaoImpl();
	Gson gson = new Gson();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String t = request.getParameter("t");
		switch (t) {
		case "getCompanyById":
			getCompanyById(request, response);
			break;
		case "getCompanyAll":
			getCompanyAll(request, response);
			break;
		case "updateTypeById":
			updateTypeById(request, response);
			break;	
		case "getCompanyByName":
			getCompanyByName(request, response);
			break;	
		case "addCompanyInformation":
			addCompanyInformation(request, response);
			break;
		case "updateCompanyInformation":
			updateCompanyInformation(request, response);
			break;
		case "updateCompanylogo":
			updateCompanylogo(request, response);
			break;
		case "updateCompanyTag":
			updateCompanyTag(request, response);
			break;
		case "updateGeneratorInformation":
			updateGeneratorInformation(request, response);
			break;
		case "updateGeneratorImg":
			updateGeneratorImg(request, response);
			break;
		case "updatePoster":
			updatePoster(request, response);
			break;
		case "updateProductInformation":
			updateProductInformation(request, response);
			break;
		case "updateCompanIintroduce":
			updateCompanIintroduce(request, response);
			break;
		default:
			break;
		}
	}

	private void getCompanyByName(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();
		String name = request.getParameter("name");
		rb.setData(dao.getCompanyByName(name));
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			// TODO Auto-generated catch block
		}
	}
	private void updateCompanIintroduce(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid=0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		String introduce = request.getParameter("introduce");
		String date =Tools.getNowDate();
		int type =0;

		CompanyBean cinfo = new CompanyBean(userid, introduce, date, type);

		if (dao.updateCompanIintroduce(cinfo) > 0) {
			rb.setMsg("修改成功");
		}else {
			rb.setMsg("修改失败");
			rb.setStatus(0);
		}
		
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void updateProductInformation(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid=0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		String product = request.getParameter("name");
		String producturl =request.getParameter("address");
		String productsummary = request.getParameter("description");
		String date =null;
		int type =0;

		CompanyBean cinfo = new CompanyBean(userid, product, producturl, productsummary, date, type);

		if (dao.updateProductInformation(cinfo) > 0) {
			rb.setMsg("修改成功");
		}else {
			rb.setMsg("修改失败");
			rb.setStatus(0);
		}
		
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void updatePoster(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid=0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		String poster = request.getParameter("imgname");
		String date =null;

		CompanyBean cinfo = new CompanyBean(userid, poster, date);

		if (dao.updatePoster(cinfo) > 0) {
			rb.setMsg("修改成功");
		}else {
			rb.setMsg("修改失败");
			rb.setStatus(0);
		}
		
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void updateGeneratorImg(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid=0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		String img = request.getParameter("imgname");

		CompanyBean cinfo = new CompanyBean(img);

		if (dao.updateGeneratorImg(cinfo,userid) > 0) {
			rb.setMsg("修改成功");
		}else {
			rb.setMsg("修改失败");
			rb.setStatus(0);
		}
		
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void updateGeneratorInformation(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid=0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		String generator = request.getParameter("name");
		String position = request.getParameter("position");
		String sina = request.getParameter("weibo");
		String summary = request.getParameter("description");

		CompanyBean cinfo = new CompanyBean(userid, generator, position, sina, summary);

		if (dao.updateGeneratorInformation(cinfo) > 0) {
			rb.setMsg("修改成功");
		}else {
			rb.setMsg("修改失败");
			rb.setStatus(0);
		}
		
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void updateCompanyTag(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid=0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		String tag = request.getParameter("labels");
		
		CompanyBean cinfo = new CompanyBean(userid, tag);

		if (dao.updateCompanyTag(cinfo) > 0) {
			rb.setMsg("修改成功");
		}else {
			rb.setMsg("修改失败");
			rb.setStatus(0);
		}
		
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void updateCompanylogo(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid=0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		String logo = request.getParameter("logoName");

		CompanyBean cinfo = new CompanyBean(logo,userid);

		if (dao.updatePoster(cinfo) > 0) {
			rb.setMsg("修改成功");
		}else {
			rb.setMsg("修改失败");
			rb.setStatus(0);
		}
		
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void updateCompanyInformation(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid=0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		String name = request.getParameter("name");
		String abbreviation = request.getParameter("workName");
		String newemail = request.getParameter("workName");
		String url = request.getParameter("name");
		String address = request.getParameter("workName");
		String field = request.getParameter("name");
		String scale = request.getParameter("workName");
		String stage = request.getParameter("workName");
		String lightspot = request.getParameter("name");

		CompanyBean cinfo = new CompanyBean(userid, name, abbreviation,  newemail, url, address, field, scale, stage, lightspot);

		if (dao.updateCompanyInformation(cinfo) > 0) {
			rb.setMsg("修改成功");
		}else {
			rb.setMsg("修改失败");
			rb.setStatus(0);
		}
		
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void addCompanyInformation(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		int userid=0;
		List<UserBean> ulist = udao.getByEmailUser(email);
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		String name = request.getParameter("companyName");
		String abbreviation = request.getParameter("companyShortName");
		String url = request.getParameter("companyUrl");
		String address = request.getParameter("city");
		String field = request.getParameter("industryField");
		String scale = request.getParameter("companySize");
		String stage = request.getParameter("financeStage");
		String lightspot = request.getParameter("companyFeatures");

		CompanyBean cinfo = new CompanyBean(userid, name, abbreviation, email, url, address, field, scale, stage, lightspot);

		if (dao.getCompanyByUserid(userid).isEmpty()) {
			if (dao.addCompanyInformation(cinfo) > 0) {
				rb.setMsg("添加成功");
			}else {
				rb.setMsg("添加失败");
				rb.setStatus(0);
			}
		} else {
			rb.setMsg("您已经注册过公司了");
			rb.setStatus(0);
		}
		
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void updateTypeById(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		int id = Integer.parseInt(request.getParameter("id"));
		int type = Integer.parseInt(request.getParameter("type"));

		CompanyBean cinfo = new CompanyBean(id, type);

		if (dao.updateTypeById(cinfo) > 0) {
			rb.setMsg("修改成功");
		}
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void getCompanyAll(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		rb.setMsg("查询成功");
		rb.setData(dao.getCompanyAll());

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void getCompanyById(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		List<UserBean> ulist = udao.getByEmailUser(email);
		int userid = 0;
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		CompanyDao cdao = new CompanyDaoImpl();
		List<CompanyBean> clist = cdao.getCompanyByUserid(userid);
		int companyid = 0;
		for (CompanyBean c : clist) {
			companyid = c.getId();
		}
		rb.setData(dao.getCompanyById(companyid));

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
